package com.xhs.controller;

import com.alibaba.fastjson2.JSONObject;
import com.xhs.dto.request.SerialNumberRequest;
import com.xhs.service.ResetRule;
import com.xhs.service.SerialNumberService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @desc:
 * @projectName: auto-number
 * @author: xhs
 * @date: 2025-03-13 周四 10:51
 * @version: JDK 21
 */
@Slf4j
@RestController
public class SerialNumberController {

    @Resource
    private SerialNumberService serialNumberService;
    @Resource
    private Map<String, ResetRule> resetRuleMap;

    @PostMapping("/serialNumber")
    public JSONObject getSerialNumber(@RequestBody SerialNumberRequest param) {
        log.info("=======请求参数:{}", JSONObject.toJSONString(param));
        // 生成规则
        String resetRuleType = param.getResetRuleType();
        // 根据resetRuleType获取对应的ResetRule
        ResetRule resetRule = resetRuleMap.get(resetRuleType);
        if(resetRule == null) {
            resetRule = resetRuleMap.get("daily");
        }
        String generate = serialNumberService.generate(param.getBusinessKey(), param.getPrefix(), param.getSuffix(), param.getLength(), resetRule);
        log.info("=======生成的编号为:{}", generate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg","success");
        jsonObject.put("data",generate);
        return jsonObject;
    }
}
